export WINEDEBUG=-all
ROOT="$(shell pwd)"
TOOLSDIR=tools
SRC=src
BIN=build
FILESYSTEM=src/filesystem
IF=$(SRC)/pages
OF=../../$(BIN)
INC=$(SRC)/inc
NAME=Rogue
TARGET=83P
#DEBUG=wxwabbitemu
DEBUG=wine $(TOOLSDIR)/Wabbitemu.exe

RED=\033[01;38;5;160m
BLUE=\033[01;34m
NORM=\033[39m

ifeq ($(TARGET),83P)
KEY=04
MODEL=0
T_STRING="83+"
else
ifeq ($(TARGET),84P)
KEY=0A
MODEL=2
T_STRING="84+"
else
ifeq ($(TARGET),83PSE)
TARGET="83P"
KEY=04
MODEL=0
T_STRING="83+SE"
else
TARGET="84P"
KEY=0A
MODEL=2
T_STRING="84+SE"
endif
endif
endif
all: filesystem pages 8xu rom info
	@echo "$(RED)Finished$(BLUE)"
info:
	@echo "$(RED)Build Info$(BLUE)"
	@echo "\tName: $(NAME)\n\tTarget: $(TARGET)\n\t\tT_STRING: $(T_STRING)\n\t\tModel: $(MODEL)\n\t\tKey: $(KEY)"
rom: 8xu
	@echo "$(RED)Creating Rom$(BLUE)"
#	wine $(TOOLSDIR)/bfbuild.exe -t $(T_STRING) -i "$(BIN)/$(NAME).8xu" -b "$(TOOLSDIR)/bf$(TARGET).hex" -o "$(BIN)/$(NAME)_$(T_STRING).rom"
	@mono $(TOOLSDIR)/HexToRom.exe -m:$(MODEL) 00:$(BIN)/bin/$(NAME).bin $(BIN)/$(NAME)_$(T_STRING).rom | awk "{print \"\t\", \$$0}";
8xu: pages
	@echo "$(RED)Creating 8xu$(BLUE)"
	@cp $(TOOLSDIR)/Build8XU.exe $(BIN)/bin/
	@cp $(TOOLSDIR)/Common.dll $(BIN)/bin/
	@cp $(TOOLSDIR)/Common.pdb $(BIN)/bin/
	@cp $(TOOLSDIR)/BigInteger.dll $(BIN)/bin/
	@cp $(TOOLSDIR)/$(KEY).key $(BIN)/bin/
	@cd $(BIN)/bin; mono Build8XU.exe -t$(TARGET) -k$(KEY) -m00 -n01 -h03 -o$(NAME).8xu -f$(KEY).key 00:0000:$(NAME).bin | awk "{print \"\t\", \$$0}";\
	mv $(NAME).8xu ..
pages:
	@echo "$(RED)Compiling pages$(BLUE)"
	@mkdir $(BIN)/bin -p
	@echo "#define COLOUR \"$(BLUE)\"\n#define NORMAL \"$(NORM)\"\n#define TI$(TARGET)\n#include \"../inc/Header.inc\"" > $(IF)/$(NAME).asm
	@echo "" > $(INC)/Header.inc;
	@ls $(INC)/ | while read LINE; do\
		if [ $$LINE != "Header.inc" ] && [ $$LINE != "Rogue.inc" ]; then\
			echo "#include \"../inc/$$LINE\"" >> $(INC)/Header.inc;\
		fi;\
	done;
	@ls $(IF)/ | while read LINE; do\
		if test -d "$(IF)/$$LINE"; then\
			echo "#include $$LINE/$$LINE.asm" >> $(IF)/$(NAME).asm;\
		fi;\
	done;
	@cp $(TOOLSDIR)/spasm $(IF)
	@cd $(IF);\
	./spasm -A -T $(NAME).asm $(OF)/bin/$(NAME).bin | awk "{print \"\t \", \$$0}";\
	rm spasm -f;
inc:
	@echo "$(RED)Creating Include File$(BLUE)"
	@mkdir $(BIN)/bin -p
	@echo "#define TI$(TARGET)\n#include \"../inc/Header.inc\"" > $(IF)/$(NAME).asm
	@echo "" > $(INC)/Header.inc;
	@ls $(INC)/ | while read LINE; do\
		if [ $$LINE != "Header.inc" ] && [ $$LINE != "Rogue.inc" ]; then\
			echo "#include \"../inc/$$LINE\"" >> $(INC)/Header.inc;\
		fi;\
	done;
	@ls $(IF)/ | while read LINE; do\
		if test -d "$(IF)/$$LINE"; then\
			echo "#include $$LINE/$$LINE.asm" >> $(IF)/$(NAME).asm;\
		fi;\
	done;
	@cp $(TOOLSDIR)/spasm $(IF)
	@cd $(IF);\
	./spasm -A -L $(NAME).asm $(OF)/bin/$(NAME).bin > /dev/null;\
	rm spasm -f;
	@mv $(BIN)/bin/$(NAME).lab $(SRC)/$(NAME).lab;
	@rm $(SRC)/$(NAME).inc -f
	@cat $(INC)/Rogue.inc | awk "{print \"\t \", \$$1}"|  while read LABEL;do\
		echo "$(BLUE)\t$$LABEL$(NORM)";\
		echo `cat $(SRC)/Rogue.lab | grep "^$$LABEL ="` >> $(SRC)/$(NAME).inc;\
	done;
	@mv $(SRC)/$(NAME).inc $(INC)/
	@mv $(SRC)/$(NAME).lab $(BIN)/bin/$(NAME).lab
filesystem: inc
	@echo "$(RED)Compiling filesystem$(BLUE)"
	@$(TOOLSDIR)/MakeFS "$(BLUE)"
clean:
	@echo "$(RED)Cleaning $(BIN)/$(NORM)"
	@rm -rf $(BIN)/*
	@rm -f $(SRC)/$(NAME).asm
debug: filesystem rom info
	@echo "$(RED)Starting debugger$(BLUE)"
	@$(DEBUG) $(BIN)/$(NAME)_$(T_STRING).rom | awk "{print \"\t\", \$$0}";
	@echo "$(RED)Finished$(NORM)"
